<?php
class Member_groupModel extends Model {
	public $list;
	public function getList($id=0,$i=0,$member_id = array()){
		$member_id['r.pid'] = $id;
		$re = $this-> alias('r') -> where($member_id) -> Field('r.*') -> select();
		if(false == $re) return null;
		foreach ($re as $v){
			$v['cl']=$i;
		    $this->list[]=$v;
			array_merge($this->list,$this->getList($v['id'],$i+1, $member_id));
		}
		return $this->list;
	}
	public function getList2($id=0,$i=0,$site_id = array()){
		$site_id['pid'] = $id;
		$re = $this-> where($site_id) -> select();
		if(false == $re) return null;
		foreach ($re as $v){
			$v['cl']=$i;
		    $this->list[]=$v;
			array_merge($this->list,$this->getList2($v['id'],$i+1, $site_id));
		}
		return $this->list;
	}
	//获取一个部门下所有职位的ID
	public function getGroupField($groupid){
		return $this->where(array("pid"=>$groupid))->getField('id',true);	
	}
	public function getGroup($pid=0){
		return $this->where(array('pid'=>$pid,'site_id'=>__SITEID__))->select();	
	}
    //复制数据
	public function copy($siteid, $tositeid,$map, $pid = 0){
		$re=$this->where($map)->order('id')->select();
		$member_accessDB = M('Member_access');
		if($re!==false){
			foreach($re as $k=>$v){
				$v['relate_id'] = $v['id'];
				$map['pid'] = $v['id'];
				$v['site_id'] = $tositeid;
				
				
				
				unset($v['id']);
				$v['pid'] = $pid;
				$pid1 = $this->data($v)->add();
				
				echo '<script>window.parent.progress("写入一个用户组，ID为：'.$pid1.'");</script>';
				ob_flush();
                flush();
				//授权
				
				$member_access = $member_accessDB -> where(array('role_id' => $v['relate_id'])) -> select();
				foreach($member_access as $k => $v){
					$v['role_id'] = $pid1;
					$member_accessDB -> add($v);
				}
				
				$this->copy($siteid, $tositeid,$map, $pid1);
			}
			return true;
		}
	}
}

?>